<template>
	<view class="container">
		<view class="form">
			<text class="title">注册</text>
			<input class="input" placeholder="请输入用户名" v-model="username" />
			<input class="input" placeholder="请输入密码" password v-model="password" />
			<input class="input" placeholder="请确认密码" password v-model="confirmPassword" />
			<button class="btn" type="primary" @click="handleRegister">注册</button>
			<view class="link">
				<text>已有账号？</text>
				<navigator url="/pages/login/login" open-type="navigateBack">去登录</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				password: '',
				confirmPassword: ''
			}
		},
		methods: {
			handleRegister() {
				if (!this.username || !this.password || !this.confirmPassword) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					})
					return
				}
				if (this.password !== this.confirmPassword) {
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '注册中...'
				})
				uniCloud.callFunction({
					name:'addUser',
					data:{
						'name':this.username,
						'password':this.password
					},
					success: (msg) => {
						console.log("addUser callback"+JSON.stringify(msg));
					},
					fail: (errer) => {
						console.error("addUser fail"+errer);
					}
				})
				setTimeout(() => {
					uni.hideLoading()
					uni.showToast({
						title: '注册成功'
					})
					uni.navigateBack()
				}, 1000)
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 70vh;
		background-color: #f9f9f9;
	}

	.form {
		width: 80%;
		padding: 40rpx;
		background: #fff;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
	}

	.title {
		font-size: 40rpx;
		text-align: center;
		margin-bottom: 40rpx;
		display: block;
	}

	.input {
		border: 1px solid #ddd;
		border-radius: 8rpx;
		padding: 20rpx;
		margin-bottom: 30rpx;
	}

	.btn {
		margin-top: 20rpx;
	}

	.link {
		margin-top: 30rpx;
		text-align: center;
		font-size: 28rpx;
		color: #666;
	}

	.link navigator {
		display: inline;
		color: #007AFF;
	}
</style>